package com.instagram.instadownloader.fragment;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.instagram.instadownloader.BaseInstaDownloaderActivity.OnDownloadCompleteListener;
import com.instagram.instadownloader.MainActivity;
import com.vine.vinedownloader.R;
import com.instagram.instadownloader.fragment.base.InstagramFragment;
import com.instagram.instadownloader.history.HistoryAdapter;
import com.instagram.instadownloader.history.HistoryAdapter.OnHistoryItemClickListener;
import com.instagram.instadownloader.history.HistoryManager;
import com.instagram.instadownloader.history.HistoryObject;
import com.instagram.instadownloader.utils.DialogUtil;
import com.instagram.instadownloader.utils.DialogUtil.OnDialogListener;
import com.instagram.instadownloader.utils.Launcher;

public class RecentDownloadFragment extends InstagramFragment implements OnDownloadCompleteListener{

	private HistoryAdapter 	mAdapter;
	private ListView		mListView;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.fragment_recent_download_tab, container, false);
		mListView = (ListView) v.findViewById(R.id.listRecentDownload);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Launcher.openFile(getActivity(), mAdapter.getItem(pos).getAbsolutePath());
			}
		});
		refreshData();
		((MainActivity)getActivity()).setOnDownloadCompleteListener(this);
		return v;
	}
	
	private void refreshData(){
			
		mAdapter = new HistoryAdapter(getActivity(), HistoryManager.getInstance().getHistoryList(), new OnHistoryItemClickListener() {
			@Override
			public void onShareClick(int pos) {
//				Toast.makeText(getActivity(), "share instagram "+pos, Toast.LENGTH_SHORT).show();
				shareInstagramImage(getActivity(),  HistoryManager.getInstance().getHistoryList().get(pos).getAbsolutePath(),
						HistoryManager.getInstance().getHistoryList().get(pos).getShareUrl());
				
			}
			
			@Override
			public void onOptionClick(final int pos) {
				DialogUtil.showConfirmDialog(getActivity(), "Confirm", "Do you want delete item?", new OnDialogListener() {
					
					@Override
					public void onPositive(String result) {
						HistoryObject object = mAdapter.getItem(pos);
						File file = new File(object.getAbsolutePath());
						if(file.exists()){
							file.delete();
						}
						HistoryManager.getInstance().removeFromHistory(object);
						mAdapter.notifyDataSetChanged();
					}
					
					@Override
					public void onCancel() {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});
		mListView.setAdapter(mAdapter);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onDownloadComplete(Intent intent) {
		mAdapter.setDatas(HistoryManager.getInstance().getHistoryList());
	}
	
	private void shareInstagramImage(Context paramContext, String absolutePath, String url)
	  {
	    if(("".equals(absolutePath)) || ("".equals(url))){
	    	return;
	    }
	    Intent localIntent = new Intent("android.intent.action.SEND");
	    localIntent.setType("image/jpeg");
	    localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
	    localIntent.putExtra("android.intent.extra.SUBJECT", "Let's check out this interesting Instagram post");
	    localIntent.putExtra("android.intent.extra.TEXT", url + " #InstaDownloader");
	    localIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(new File(absolutePath)));
	    paramContext.startActivity(Intent.createChooser(localIntent, "Share image via"));
	  }

}
